package com.kehao.leetcode.order;

public class Leetcode123 {
    public int maxProfit(int[] prices) {
        int dp[][] = new int[prices.length][5];//手上的资金数
        dp[0][1] = -prices[0];
        dp[0][3] = -prices[0];//第一天买了又卖又买
        for (int i = 1; i < prices.length; i++) {
            for (int j = 1; j < 5; j++) {
                if(j%2==1){//买
                    dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-1]-prices[i]);
                }else{//卖
                    dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-1]+prices[i]);
                }
            }
        }
        return dp[prices.length-1][4];
    }
}
